import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import SkillBtn from "./SkillBtn";


const { ccclass, property } = cc._decorator;
//关卡中的控制器，接收玩家操作
@ccclass
export default class LevelController extends yyComponent {

    public init() {
        this.initTouchState();
        this.onEvents();
    }
    protected onEvents() {
        this.on(EventType.Common.CtrlEvent.ctrlStart, this.setEnable, this);
        this.on(EventType.Common.CtrlEvent.ctrlEnd, this.setDisable, this);

        this.node.on(cc.Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this.onTouchEnd, this);
    }

    public reset() {
        this.resetTouchState();
    }

    //启用触摸操作层
    public setEnable() {
        this.reset();
        this.node.active = true;
    }
    //禁用触摸操作层
    public setDisable() {
        this.reset();
        this.node.active = false;
    }

    //#region 触摸事件
    protected touched: boolean = false;
    protected touchStartPos: cc.Vec2;
    protected cachePos: cc.Vec2;
    protected initTouchState() {
        this.touched = false;
        this.touchStartPos = cc.v2();
        this.cachePos = cc.v2();
    }
    protected resetTouchState() {
        this.touched = false;
    }
    protected onTouchStart(e: cc.EventTouch) {
        this.touched = true;
        e.getUILocation(this.touchStartPos);
        this.emit(EventType.LevelEvent.startJoy);
    }
    protected onTouchMove(e: cc.EventTouch) {
        if (!this.touched) return;
        e.getUILocation(this.cachePos);
        this.emit(EventType.LevelEvent.moveJoy, this.cachePos.x - this.touchStartPos.x);
    }
    protected onTouchEnd(e: cc.EventTouch) {
        if (!this.touched) return;
        this.touched = false;
        this.emit(EventType.LevelEvent.stopJoy);
    }
    //#endregion
}
